package com.atos.smp.actions;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.primefaces.component.dialog.Dialog;
import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.ItemSelectEvent;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.LineChartSeries;
import org.primefaces.model.chart.PieChartModel;

import com.atos.smp.beans.Collaborator;
import com.atos.smp.beans.Process;
import com.atos.smp.beans.ProcessesAVGMapper;
import com.atos.smp.business.CollaboratorManager;
import com.atos.smp.business.ProcessManager;

@ManagedBean
@SessionScoped
public class ProcessesBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private ProcessManager processManager;

	private String das;
	private Date date1;
	private List<ProcessesAVGMapper> processesList;// model moyenne des process
	private CartesianChartModel processes;

	private List<Process> processInformation;// variation des process dans le
												// temps
	private CartesianChartModel processesLine;

	private HashMap<Integer, String> mapE = new HashMap<Integer, String>();
	private CollaboratorManager collaboratorManager;
	private String collaboratorName;
	private String processName = "eclipse.exe";

	public ProcessesBean() {
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		AuthentificationBean bean = (AuthentificationBean) session
				.getAttribute("authentificationBean");
		das = bean.getCollaborator().getDas();
		System.out.println(">>>>>>" + das);

		processManager = Action.getProcessManager();
		collaboratorManager = Action.getCollaboratorManager();
		Collaborator c = collaboratorManager.findCollaboratorByDas(das);
		collaboratorName = c.getFirstName() + " " + c.getLastName();
		createProcessesChartModel();
		// linear
		processesLine = new CartesianChartModel();

	}

	private void createProcessesChartModel() {
		processes = new CartesianChartModel();
		// tous les processus d'un collaborateur
		processesList = processManager.findCollaboratorProcesses(das,
				getDate1());
		ChartSeries process = new ChartSeries();
		process.setLabel("Processus ");
		for (int i = 0; i < processesList.size(); i++) {
			process.set(processesList.get(i).getName().toString(),
					processesList.get(i).getMemory());
			mapE.put(i, processesList.get(i).getName());

		}

		processes.addSeries(process);

	}

	private void createLinearModel(String nameProcess) {
		processesLine = new CartesianChartModel();
		processInformation = processManager.findCollaboratorProcessByName(
				"eclipse", das, getDate1());
		System.out.println("createLinearModel");

		LineChartSeries series1 = new LineChartSeries();
		series1.setLabel("Process Name");

		Calendar calendar = GregorianCalendar.getInstance();
		for (int i = 0; i < processInformation.size(); i++) {

			calendar.setTime(processInformation.get(i).getDate());

			series1.set(
					calendar.get(Calendar.HOUR) + ","
							+ calendar.get(Calendar.MINUTE) + ","
							+ calendar.get(Calendar.SECOND), processInformation
							.get(i).getMemory());

		}
		processesLine.addSeries(series1);
	}

	public ProcessManager getProcessManager() {
		return processManager;
	}

	public void setProcessManager(ProcessManager processManager) {
		this.processManager = processManager;
	}

	public String getDas() {
		return das;
	}

	public String getCollaboratorName() {
		return collaboratorName;
	}

	public void setCollaboratorName(String collaboratorName) {
		this.collaboratorName = collaboratorName;
	}

	public List<ProcessesAVGMapper> getProcessesList() {
		return processesList;
	}

	public void setProcessesList(List<ProcessesAVGMapper> processesList) {
		this.processesList = processesList;
	}

	public CartesianChartModel getProcesses() {
		return processes;
	}

	public void setProcesses(CartesianChartModel processes) {
		this.processes = processes;
	}

	public String getProcessName() {
		return processName;
	}

	public void setProcessName(String processName) {
		this.processName = processName;
	}

	public void setDas(String das) {
		this.das = das;
		processInformation = processManager.findCollaboratorProcessByName(
				"eclipse", das, getDate1());
		Collaborator c = collaboratorManager.findCollaboratorByDas(das);
		collaboratorName = c.getFirstName() + " " + c.getLastName();
		createProcessesChartModel();

	}

	public List<Process> getProcessInformation() {
		return processInformation;
	}

	public void setProcessInformation(List<Process> processInformation) {
		this.processInformation = processInformation;
	}

	public void handleDateSelect(DateSelectEvent event) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		SimpleDateFormat format = new SimpleDateFormat("d/M/yyyy");
		facesContext.addMessage(null, new FacesMessage(
				FacesMessage.SEVERITY_INFO, "Date Selected", event.getSource()
						.toString()));
	}

	public void itemSelect(ItemSelectEvent event) {
		// FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
		// "Item selected",
		// "Item Index: " + event.getItemIndex() + ", Series Index:" +
		// event.getSeriesIndex());
		//
		// FacesContext.getCurrentInstance().addMessage(null, msg);
		System.out.println(event.getItemIndex());
		processName = mapE.get(event.getItemIndex());
		System.out.println(">>>>>>>>>>>>>>>>>>>>" + processName);
		createLinearModel(processName);
	}

	public CartesianChartModel getProcessesLine() {
		createLinearModel(processName);
		return processesLine;

	}

	public void setProcessesLine(CartesianChartModel processesLine) {
		this.processesLine = processesLine;
	}

	public void setInfo(String das2, Date date1) {
		System.out.println("injection de parametre");
		setDate1(date1);
		setDas(das2);

	}

	public Date getDate1() {
		return date1;
	}

	public void setDate1(Date date1) {
		this.date1 = date1;
	}

}
